#!/bin/bash

# 单独执行：inotifywait -mrq -e 'create,delete,close_write,attrib,moved_to' --timefmt '%Y-%m-%d %H:%M:%S' --format '%T %w%f %e' /home/stock
INOTIFY_CMD="inotifywait -mrq -e create,delete,close_write,attrib,moved_to /home/stock"
RSYNC_CMD="rsync -avz --delete --password-file=/etc/rsync.passwd  /home/stock rsync_backup@172.26.214.245::home"

$INOTIFY_CMD | while read DIRECTORY EVENT FILE
do
    if [ $(pgrep rsync | wc -l) -le 0 ] ; then
        current_time=`date +"%Y-%m-%d %H:%M:%S"`
        echo "========= $current_time"
        #echo $(date +%Y-%m-%d\ %H:%M:%S)
        $RSYNC_CMD
    fi
done